package com.alm.token.utils;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;
import com.alm.token.R;


/**
 * Created by nelson on 16/4/19.
 */
public class ActionBarHelper {

    private Activity mContext;

    public ActionBarHelper(Activity activity) {
        this.mContext = activity;
        setBackDefault();
    }

    private void setBackDefault() {
        ImageView leftView = (ImageView) mContext.findViewById(R.id.leftItem);
        leftView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideKeyboard();

                mContext.finish();
            }
        });
    }

    /**
     * 隐藏软键盘
     */
    protected void hideKeyboard() {
        if (mContext.getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (mContext.getCurrentFocus() != null) {
                InputMethodManager inputManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(mContext.getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    public ActionBarHelper setTitle(String title) {
        TextView textView = (TextView) mContext.findViewById(R.id.centerItem);
        textView.setText(title);
        return this;
    }

    public ActionBarHelper setTitle(String title1,String title2) {
        TextView textView1 = (TextView) mContext.findViewById(R.id.centerItem1);
        textView1.setText(title1);
        TextView textView2 = (TextView) mContext.findViewById(R.id.centerItem2);
        textView2.setText(title2);
        return this;
    }

    public ActionBarHelper setTitleIcon(int resId) {
        TextView textView = (TextView) mContext.findViewById(R.id.centerItem);
        textView.setBackgroundResource(resId);
        return this;
    }

    public ActionBarHelper setIcon(int resId) {
        TextView textView = (TextView) mContext.findViewById(R.id.centerItem);
        textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, resId, 0);
        return this;
    }

    public ActionBarHelper setTitle(int resId) {
        TextView textView = (TextView) mContext.findViewById(R.id.centerItem);
        textView.setText(resId);
        return this;
    }

    public ActionBarHelper setTitleColor(int colorId) {
        TextView textView = (TextView) mContext.findViewById(R.id.centerItem);
        textView.setTextColor(colorId);
        return this;
    }

    public ActionBarHelper hideBack(boolean flag) {
        ImageView leftView = (ImageView) mContext.findViewById(R.id.leftItem);
        leftView.setVisibility(flag ? View.GONE : View.VISIBLE);
        return this;
    }

    public ActionBarHelper hideBack(boolean flag, View.OnClickListener onClickListener) {
        ImageView leftView = (ImageView) mContext.findViewById(R.id.leftItem);
        leftView.setVisibility(flag ? View.GONE : View.VISIBLE);
        leftView.setOnClickListener(onClickListener);
        return this;
    }

    public ActionBarHelper setRightIcon(int resId) {
        TextView textView = (TextView) mContext.findViewById(R.id.rightItem);
        textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, resId, 0);
        return this;
    }

    public ActionBarHelper setLeft(View.OnClickListener onClickListener) {
        ImageView leftView = (ImageView) mContext.findViewById(R.id.leftItem);
        leftView.setOnClickListener(onClickListener);
        return this;
    }

    public ActionBarHelper setLeftIcon(int resId, View.OnClickListener onClickListener) {
        ImageView leftView = (ImageView) mContext.findViewById(R.id.leftItem);
        leftView.setImageResource(resId);
        return this;
    }

    public ActionBarHelper setLeftIcon(int resId) {
        ImageView leftView = (ImageView) mContext.findViewById(R.id.leftItem);
        leftView.setImageResource(resId);
        return this;
    }

    public ActionBarHelper setRightIcon(int resId, View.OnClickListener onClickListener) {
        TextView textView = (TextView) mContext.findViewById(R.id.rightItem);
        textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, resId, 0);
        textView.setOnClickListener(onClickListener);
        return this;
    }

    public ActionBarHelper setRightText(String text, View.OnClickListener onClickListener) {
        TextView textView = (TextView) mContext.findViewById(R.id.rightItem);
        textView.setText(text);
        textView.setOnClickListener(onClickListener);
        return this;
    }

    public ActionBarHelper setRightText(String text) {
        TextView textView = (TextView) mContext.findViewById(R.id.rightItem);
        textView.setText(text);
        return this;
    }

    public ActionBarHelper setRightListener(View.OnClickListener onClickListener) {
        TextView textView = (TextView) mContext.findViewById(R.id.rightItem);
        textView.setOnClickListener(onClickListener);
        return this;
    }

    public ActionBarHelper setRightTextColor(int colorId) {
        TextView textView = (TextView) mContext.findViewById(R.id.rightItem);
        textView.setTextColor(colorId);
        return this;
    }

    public ActionBarHelper setRightText(boolean enabled) {
        TextView textView = (TextView) mContext.findViewById(R.id.rightItem);
        textView.setEnabled(enabled);
        return this;
    }

    public ActionBarHelper setRightText(int resId, View.OnClickListener onClickListener) {
        setRightText(mContext.getString(resId), onClickListener);
        return this;
    }

    public ActionBarHelper showRightIcon() {
        TextView textView = (TextView) mContext.findViewById(R.id.rightItem);
        textView.setVisibility(View.VISIBLE);
        return this;
    }

    public ActionBarHelper hideRightIcon() {
        TextView textView = (TextView) mContext.findViewById(R.id.rightItem);
        textView.setVisibility(View.GONE);
        return this;
    }
}
